// pages/member/integral/integral.js
var app = getApp();
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    credit1: 0.00,
    transfer: false, //是否开启转账
    activity: false, //是否开启积分活动,
    integral: '',
    transfer_integral: false, //积分转化消费积分
    transfer_integral_text: '',
    loveByMember: 0,
    consumption_integral:'',
    love: '',

    poster_id: null, //海报id
    iPnoneBottomBol: false,
    showcarrfootBol: true,
    isshow:false,

    page: 1,
    isLoadMore: true,
    total_page: 0,
    imgPath: app.globalData.imgPath
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._getIntegral();
    try {
      const value = wx.getStorageSync('integral');
      const {integral:consumption_integral} = wx.getStorageSync('yz_basic_info')
      if(consumption_integral){
        this.setData({consumption_integral})
      }
      if (value) {
        this.setData({
          integral: wx.getStorageSync('integral'),
        });
        // Do something with return value
        if (this.data.integral) {
          wx.setNavigationBarTitle({
            title: this.data.integral
          });
        }
      }
    } catch (e) {
      // Do something when catch error
    }

    let info_basic = wx.getStorageSync('yz_basic_info');
    this.setData({
      love: info_basic.love
    })
    console.log(info_basic);
    this.getiPnoneBottom();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getiPnoneBottom() {
    let that = this;
    try {
      var value = wx.getStorageSync("iPhoneBottom");
      if (value) {
        that.setData({
          iPnoneBottomBol: true,
        });
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  showTap() {
    this.setData({
      show: true
    })
  },
  showClose() {
    this.setData({
      show: false
    })
  },
  jumpUrlBtn(e){
    let item = e.currentTarget.dataset.item;
    if (item.type == 1) {
      // 自定义链接
      wx.navigateTo({
        url: item.mini_url,
        fail:function(err){
          wx.reLaunch({
            url: item.mini_url,
          })
        }
      })
    } else if (item.type == 2) {
      // 广告
      if (item.poster_id) {
        // 小程序不显示关注海报
        // this.getPoster(item.poster_id)
        // console.log(item.poster_id,'ddddddddddd')
        // this.setData({
        //   isshow:true,
        //   poster_id:item.poster_id
        // })
      }
    } else if (item.type == 3) {
      wx.navigateTo({
        url: '/packageA/member/editmobile/editmobile',
      })
    }else{
      wx.navigateTo({
        url: item.mini_url,
        fail:function(err){
          wx.reLaunch({
            url: item.mini_url,
          })
        }
      })
    }
  },
  // closeMethod(e){
  //   this.setData({
  //     isshow:e.detail.isshow
  //   })
  // },
  tapOther(e){
    let index = e.currentTarget.dataset.index;
    if (index == 'activity') {
      this.toIntegral();
    } else if (index == 'transfer_integral') {
      this.to_transfer_integral();
    } else if (index == 'exchange_to_love_by_member') {
      this.to_transfer_love();
    } else if(index == 'point_exchange'){
      this.point_exchange()
    }

  },
  point_exchange(){
    wx.navigateTo({
      url: '/packageI/Intergral_transform/Intergral_transform',
    })
  },
  // getPointTransfer() {
  //   app._postNetWork({
  //     url: app.getNetAddresss("finance.point-transfer-integral.page-per"),
  //     success: res => {
  //       if (res.data.result) {
  //         let text = res.data.data.point_name + '转化' + res.data.data.integral_name;
  //         this.setData({ transfer_integral_text: text })
  //       }
  //     }
  //   })
  // },
  getTaskList() {
    app._postNetWork({
      url: app.getNetAddresss("plugin.point-task.api.task.index"),
      success: res => {
        if (res.data.result) {
          this.setData({
            listIncome: res.data.data.income,
            list: res.data.data.list,
            integralTaskName:res.data.name?res.data.name:'积分任务'
          })
          this.data.isLoadMore = true;
          this.data.total_page = res.data.last_page;
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
        }
      }
    })
  },
  getMoreData() {
    let urlStr = "";
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      urlStr = app.getNetAddresss('plugin.point-task.api.task.index');
      app._postNetWork({
        url: urlStr,
        data: {
          page: this.data.page,
        },
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              list: this.data.list.concat(res.data.data.list)
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  _getIntegral() {
    let that = this;
    let urlStr = app.getNetAddresss("finance.point-page.index");
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            credit1: res.data.credit1,
            transfer: res.data.transfer,
            transfer_integral: res.data.transfer_integral,
            // loveByMember: res.data.exchange_to_love_by_member,
            dataList: res.data.latest_record,
            other: res.data.other,
            point_task_switch:res.data.point_task_switch
          });
          if(res.data.point_task_switch){
            that.getTaskList();
          }
          if(res.data.other.exchange_to_love_by_member){
            that.data.loveByMember = 1;
          }
          // if (res.data.transfer_integral) {
            // that.getPointTransfer()
          // }
          if (res.data.activity == 1) {
            that.setData({
              activity: true
            });
          } else {
            that.setData({
              activity: false
            });
          }
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: that.data.themeBaseColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //转账的响应方法
  transferHandel() {
    if (this.data.transfer) {
      wx.navigateTo({
        url: '/packageB/member/integral_transfer/integral_transfer?integral=' + this.data.credit1
      });
    } else {
      wx.showToast({
        icon: 'none',
        title: '您的转账功能未开启',
        duration: 1500
      });
    }
  },
  to_transfer_integral() {
    wx.navigateTo({
      url: '/packageB/member/transfer_integral/transfer_integral'
    });
  },
  to_transfer_love() {
    wx.navigateTo({
      url: `/packageB/member/transfer_integral/transfer_integral?loveByMember=${this.data.loveByMember}`
    });
  },
  toIntegral() {
    wx.navigateTo({
      url: '/packageB/member/IntegralActivity/IntegralActivity?integral=' + this.data.credit1
    });
  },
  integralgolist() {
    wx.navigateTo({
      url: '/packageB/member/integrallist/integrallist'
    });
  },
});
